//
//  ActivityTableViewController.m
//  fupengdouban
//
//  Created by lanou3g on 15/10/23.
//  Copyright © 2015年 fuoeng. All rights reserved.
//

#import "ActivityTableViewController.h"
#import "ActivityTableViewCell.h"
#import "DownLoad.h"
#import "Message.h"
#import "ActViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"

@interface ActivityTableViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, strong)NSMutableArray * dataArray;



@end

@implementation ActivityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"活动";
    
    
    [self.tableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    
    self.dataArray = [NSMutableArray array];
    
    [DownLoad downLoadWithURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php" method:@"GET" param:nil passValue:^(id data) {
        
        NSDictionary * tempDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"%@",tempDict);

        NSArray * array = tempDict[@"events"];
        
        
        for (NSDictionary * dict in array) {

            Message * m = [[Message alloc] init];
            [m setValuesForKeysWithDictionary:dict];
            
            [self.dataArray addObject:m];

        }
        dispatch_async(dispatch_get_main_queue(), ^{
          [self.tableView reloadData];
            
        });
  
        
    }];
    
    [self example01];
    [self example02];

   
}

- (void)example01
{
    __weak __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
    // 马上进入刷新状态
    [self.tableView.header beginRefreshing];
}


-(void)example02
{
    __weak __typeof(self) weakSelf = self;
    
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    
}


- (void)loadNewData
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.tableView.header endRefreshing];
        [self.tableView.footer endRefreshing];
    });
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Message * m = self.dataArray[indexPath.row];
    cell.titleLabel.text = m.title;
    
    NSRange x ={5,11};
    NSString * s1 = [m.begin_time substringWithRange:x];
    NSString * s2 = [m.end_time substringWithRange:x];
    
    cell.beginEndTimeLabel.text = [NSString stringWithFormat:@"%@ -- %@", s1,s2];
    
    cell.AddressLabel.text = m.address;
    cell.CategoryNameLabel.text = [NSString stringWithFormat:@"类型 : %@", m.category_name];
    cell.WisherCountLabel2.text = [NSString stringWithFormat:@"%@",m.wisher_count];
    cell.ParticipantCountLabel2.text = [NSString stringWithFormat:@"%@",m.participant_count];
    
    
    [cell.imv sd_setImageWithURL:[NSURL URLWithString:m.image] placeholderImage:[UIImage imageNamed:@"dlam.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        m.activityImg = image;
        

    }];
    
    
    return cell;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 190;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActViewController * actVC = [[ActViewController alloc] init];
    Message * m = self.dataArray[indexPath.row];
    
    actVC.title = m.title;
    actVC.message = m;
    
    
    [self.navigationController pushViewController:actVC animated:YES];
    
}



@end
